const util = require('../../utils/util');
const MaterialTypeService = require('../../service/admin/basic_material_type.service');

async function createMaterialType(ctx) {
  const { userId } = util.decodeToken(ctx);
  const { parent_id = 0, category_code, category_name, category_level, sort, remark, status } = ctx.request.body;

  if (!category_name) {
    return util.fail(ctx, '分类名称不能为空');
  }

  try {
    const result = await MaterialTypeService.createMaterialType({
      parent_id,
      category_code,
      category_name,
      category_level,
      sort,
      remark,
      status,
      create_id: userId,
      updata_id: userId
    });
    util.success(ctx, { id: result.id });
  } catch (error) {
    console.error('Create material type error:', error.message);
    util.fail(ctx, error.message || '创建失败');
  }
}

async function deleteMaterialTypes(ctx) {
  const { ids } = ctx.request.body;

  if (!Array.isArray(ids) || ids.length === 0) {
    return util.fail(ctx, '分类 ID 列表无效');
  }

  try {
    await MaterialTypeService.deleteMaterialTypes(ids);
    util.success(ctx, '');
  } catch (error) {
    console.error('Delete material types error:', error.message);
    util.fail(ctx, error.message || '删除失败');
  }
}

async function updateMaterialType(ctx) {
  const { userId } = util.decodeToken(ctx);
  const { id, parent_id, category_code, category_name, category_level, sort, remark, status } = ctx.request.body;

  if (!id || !category_name) {
    return util.fail(ctx, '分类 ID 或名称不能为空');
  }

  try {
    await MaterialTypeService.updateMaterialType({
      id,
      parent_id,
      category_code,
      category_name,
      category_level,
      sort,
      remark,
      status,
      updata_id: userId
    });
    util.success(ctx, '');
  } catch (error) {
    console.error('Update material type error:', error.message);
    util.fail(ctx, error.message || '更新失败');
  }
}

async function getMaterialTypeList(ctx) {
  const { page = 1, limit = 10, category_name, category_code } = ctx.query;

  try {
    const result = await MaterialTypeService.getMaterialTypeList({
      page: parseInt(page),
      limit: parseInt(limit),
      category_name,
      category_code
    });
    util.success(ctx, result);
  } catch (error) {
    console.error('Get material type list error:', error.message);
    util.fail(ctx, error.message || '查询失败');
  }
}

async function updateMaterialTypeState(ctx) {
  const { userId } = util.decodeToken(ctx);
  const { id, status } = ctx.request.body;

  if (!id) {
    return util.fail(ctx, '分类 ID 无效');
  }
  const newState = status === 1017001 ? 1017002 : 1017001;
  try {
    await MaterialTypeService.updateMaterialTypeState({ id, newState, updata_id: userId });
    util.success(ctx, '');
  } catch (error) {
    console.error('Update material type status error:', error.message);
    util.fail(ctx, error.message || '状态更新失败');
  }
}
async function batchCreateMaterialType(ctx) {
  const { userId } = util.decodeToken(ctx);
  const { parent_id, data } = ctx.request.body;

  if (!parent_id || !Array.isArray(data) || data.length === 0) {
    return util.fail(ctx, '父级 ID 或数据列表无效');
  }
  if (data.some(item => !item.category_name)) {
    return util.fail(ctx, '分类名称不能为空');
  }

  try {
    const result = await MaterialTypeService.batchCreateMaterialType({
      parent_id,
      data,
      create_id: userId,
      updata_id: userId
    });
    util.success(ctx, { ids: result.ids });
  } catch (error) {
    console.error('Batch create material type error:', error.message);
    util.fail(ctx, error.message || '批量创建失败');
  }
}
async function getMaterialTypeCascade(ctx) {
  const { keyword } = ctx.query;

  try {
    const result = await MaterialTypeService.getMaterialTypeCascade({ keyword });
    util.success(ctx, result);
  } catch (error) {
    console.error('Get material type cascade error:', error.message);
    util.fail(ctx, error.message || '查询级联数据失败');
  }
}
module.exports = {
  createMaterialType,
  deleteMaterialTypes,
  updateMaterialType,
  getMaterialTypeList,
  updateMaterialTypeState,
  batchCreateMaterialType,
  getMaterialTypeCascade
};
